//
//  CampusListCell.m
//  ShowYork
//
//  Created by hxh on 2021/3/25.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "CampusListItemCell.h"
#import "CampusItemTypeFlowLayout.h"
#import "CampusListItemTypeCell.h"
#import "CampusConfigModel.h"
#import "CampusParamItemModel.h"
@interface CampusListItemCell ()<CampusItemTypeFlowLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIImageView *icon;
@property (nonatomic,strong)UILabel *mainTitleLb;
@property (nonatomic,strong)UILabel *subTitleLb;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIButton *coverBtn;
@end

@implementation CampusListItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.icon = [[UIImageView alloc]init];
    self.icon.contentMode = UIViewContentModeScaleAspectFill;
    self.icon.layer.cornerRadius = 19;
    self.icon.layer.masksToBounds = YES;
    self.icon.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.icon];
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(16);
        make.left.equalTo(self.contentView).offset(18);
        make.size.mas_equalTo(CGSizeMake(38, 38));
    }];
    
    self.mainTitleLb = [[UILabel alloc]init];
    self.mainTitleLb.textColor = C51;
    self.mainTitleLb.font = [UIFont boldSystemFontOfSize:14];
    [self.contentView addSubview:self.mainTitleLb];
    [self.mainTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(16);
        make.left.equalTo(self.icon.mas_right).offset(12);
        make.right.mas_lessThanOrEqualTo(self.contentView);
    }];
    self.subTitleLb = [[UILabel alloc]init];
    self.subTitleLb.textColor = C102;
    self.subTitleLb.font = font(12);
    [self.contentView addSubview:self.subTitleLb];
    [self.subTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainTitleLb).offset(2);
        make.left.equalTo(self.mainTitleLb.mas_right).offset(12);
    }];
    
    CampusItemTypeFlowLayout *layout = [CampusItemTypeFlowLayout new];

    layout.labelFont = font(12);
    //自定义layout初始化
    layout.delegate = self;
    layout.lineSpacing = 4.0f;
    layout.interitemSpacing = 8.0f;
    layout.headerViewHeight = 0;
    layout.footerViewHeight = 0;
    layout.itemHeight = 16.0;
    layout.maxWidth = 70 + 16;
    layout.itemInset = UIEdgeInsetsZero;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.collectionView registerClass:[CampusListItemTypeCell class] forCellWithReuseIdentifier:@"CampusListItemTypeCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainTitleLb.mas_bottom).offset(4);
        make.left.equalTo(self.mainTitleLb).offset(-4);
        make.right.equalTo(self.contentView).offset(-12);
        make.bottom.equalTo(self.contentView).offset(-4);
        make.height.mas_equalTo(@0).priorityHigh();
    }];

    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(19);
        make.right.equalTo(self.contentView).offset(-19);
        make.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(@0.5);
    }];
    self.line.hidden = YES;
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemModel.hierarchyArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CampusListItemTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CampusListItemTypeCell" forIndexPath:indexPath];
    [cell setItemType:self.itemModel.hierarchyArray[indexPath.item]];
    return cell;
}

- (NSString *)collectionViewItemSizeWithIndexPath:(NSIndexPath *)indexPath{
    return self.itemModel.hierarchyArray[indexPath.item];
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"CampusListItemCell";
    CampusListItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CampusListItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setItemModel:(CampusSchoolItemModel *)itemModel{
    _itemModel = itemModel;
    self.mainTitleLb.text = itemModel.name;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:itemModel.logo]];
    
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(itemModel.itemH).priorityHigh();
    }];
    
    CampusConfigModel *configModel = [CampusConfigModel getWithKey:@"type"];
    
    __block NSString *name = @"";
    [configModel.paramList enumerateObjectsUsingBlock:^(CampusParamItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.name isEqualToString:itemModel.type.stringValue]) {
            name = obj.value;
            *stop = YES;
        }
    }];
    
    if (name.length != 0) {
        self.subTitleLb.text = [NSString stringWithFormat:@"%@·%@",itemModel.city,name];
    }else{
        self.subTitleLb.text = itemModel.city;
    }
    
    [self.collectionView reloadData];
    
    self.line.hidden = !itemModel.showLine;
    
    CGSize size = [RPAppManager getTextSizeWithText:self.subTitleLb.text font:font(12) maxSize:CGSizeMake(kScreenW, MAXFLOAT)];
    
    [self.mainTitleLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_lessThanOrEqualTo(self.contentView).offset(-(ceil(size.width) + 19 + 12));
    }];
    
}

- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showCampusDetailWithItemModel:)]) {
        [self.delegate showCampusDetailWithItemModel:self.itemModel];
    }
}
@end
